import type { App } from "vue"
import { createLogger, createStore } from "vuex"
import { loadToken, saveToken } from "../utils/cache"
import state from "./module/state"

const store = createStore({
  state: {
    ...state,
    token: loadToken()
  },
  mutations: {
    SET_TOKEN(state, token) {
      saveToken(token)
      state.token = token
    }
  },
  actions: {},
  modules: {},
  plugins: [createLogger()]
})

export function setupAppstore(app: App<Element>) {
  app.use(store)
}

export function hotModuleUnregisterModule(name: string) {
  if (!name) return
  if ((store.state as any)[name]) {
    store.unregisterModule(name)
  }
}

export default store
